/**
 * Created by fdg296 on 2017/9/29.
 */
$(document).ready(function(){

    $('#table_id_example').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/goods-category/get-brand-group',
            type:'post',
            data:function(d){
                d.group_name = $('#group_name').val();
            },
            error:getError,
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "group_name", "bSortable": false },
            { "data": "group_alias", "bSortable": false },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="updateBrandGroup('+ full.id +')">修改</a>';
                }
            },
            {
                "targets": [1],
                "searchable":false
            },
            {
                "targets": [4],
                "data": "use_flg",
                "render": function(data, type, full) {
                    if(data == 1){
                        return '<a href="javascript:" onclick="disableClick('+ full.id +')">禁用</a>';
                    }else{
                        return '<a href="javascript:" onclick="enableClick('+ full.id +')">启用</a>';
                    }
                }
            }
        ]
    });
});

$('#select_button').click(function(){
    var table = $('#table_id_example').DataTable();
    table.ajax.reload();
})

$('#reset_button').click(function(){
    $('#group_name').val('');
    var table = $('#table_id_example').DataTable();
    table.ajax.reload();
})

$('#depart_name').click(function(){
    if($('#treeDemo').hasClass('ul_tree_display')){
        $('#treeDemo').removeClass('ul_tree_display');
    }else{
        $('#treeDemo').addClass('ul_tree_display');
    }
})

function updateBrandGroup(id){
    $('.mod-brand-class').each(function(){
        $(this).prop('checked',false);
    })
    var role_array = new Array();
    $('#id').val(id);
    $.post('/goods-category/get-one-brand-group',{id:id},success,'json');
    return false;
    function success(data){
        if(data == null){
            $('#update-brand-modal').modal('show');
        }else{
            if(data.code == 500){
                $.jBox.error(data.val, '错误');
            }else{
                $('#id').val(data.id);
                $('#group_name_update').val(data.group_name);
                $('#group_alias').val(data.group_alias);
                $('#use_flg').val(data.use_flg);
                if(data.brand_id_list != ''){
                    var role_string = data.brand_id_list;
                    $('#old_brand_id').val(role_string);
                    role_array = role_string.split(',');
                    $('.mod-brand-class').each(function(){
                        var check_value = $(this).val();
                        var array_num = $.inArray(check_value, role_array);
                        if(eval(array_num) >= 0){
                            $(this).prop('checked',true);
                        }
                    })
                }
                $('#update-brand-modal').modal('show');
            }
        }
    }
}

$('.ajax_update_choose_brand').click(function(){
    var group_name_update = $('#group_name_update').val();

    if(group_name_update == ''){
        $.jBox.error('请输入品牌组名称', '错误');
        return false;
    }

    $.post($('#brand_form').attr('action'),$('#brand_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { self.location.reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

$('#add_button').click(function(){
    $('#choose-brand-modal').modal('show');
})

$('.ajax_add_choose_brand').click(function(){
    var group_name_other = $('#group_name_other').val();
    if(group_name_other == ''){
        $.jBox.error('请输入品牌组名称', '错误');
        return false;
    }

    $.post($('#group_form').attr('action'),$('#group_form').serialize(),success,'json');
    return false;
    function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () { self.location.reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
})

//禁用
function disableClick(id){
    $.jBox.confirm("确定禁用吗？", "提示", function(v){
        if (v == 'ok'){
            $.post('/goods-category/disable-brand',{id:id},success,'json');
            return false;
            function success(data){
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { self.location.reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
        }
    });
}

//启用
function enableClick(id){
    $.jBox.confirm("确定启用吗？", "提示", function(v){
        if (v == 'ok'){
            $.post('/goods-category/enable-brand',{id:id},success,'json');
            return false;
            function success(data){
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { self.location.reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
        }
    });
}